<?php
require_once("lib/db_connection.php");
$job_id = $_GET['job_id'];
$company_id = $_SESSION['company_id'];

function commafy($_)
       {
        return strrev( (string)preg_replace( '/(\d{3})(?=\d)(?!\d*\.)/', '$1,' , strrev( $_ ) ) );
       }

$result_select = mysql_query("SELECT * FROM rou_job_loc_tbl WHERE job_id='".$job_id."'")
or die("SELECT LOCS ".mysql_error());
$rcount = mysql_num_rows($result_select);

$result_tot_od = mysql_query("SELECT SUM(miles) as sum_miles FROM rou_job_loc_tbl WHERE job_id='".$job_id."'")
or die("SELECT OD ".mysql_error());
$row_tot_od = mysql_fetch_array($result_tot_od);

$result_tot_hrs = mysql_query("SELECT SUM(total_hrs) as sum_hrs FROM rou_job_loc_tbl WHERE job_id='".$job_id."'")
or die("SELECT OD ".mysql_error());
$row_tot_hrs = mysql_fetch_array($result_tot_hrs);

$tot_hrs = $row_tot_hrs['sum_hrs'];

$result_num = mysql_query("SELECT * FROM route_tbl WHERE route_id='".$job_id."'")
or die("SELECT NUM ".mysql_error());
$row_num = mysql_fetch_array($result_num);

$result_fuel = mysql_query("SELECT SUM(tot_cost) as sum_fuel FROM rou_fuel_tbl WHERE route_id='".$job_id."'")
or die("SELECT FUEL ".mysql_error());
$row_fuel = mysql_fetch_array($result_fuel);

$result_fuel_exp = mysql_query("SELECT * FROM rou_fuel_tbl WHERE route_id='".$job_id."'")
or die("SELECT FUEL ".mysql_error());

$result_expense = mysql_query("SELECT SUM(amount) as sum_expense FROM rou_expense_tbl WHERE route_id='".$job_id."'
AND acct_type='Expense'") or die("SELECT FUEL ".mysql_error());
$row_expense = mysql_fetch_array($result_expense);

$result_income = mysql_query("SELECT SUM(amount) as sum_expense FROM rou_expense_tbl WHERE route_id='".$job_id."'
AND acct_type='Income'") or die("SELECT FUEL ".mysql_error());
$row_income = mysql_fetch_array($result_income);

//$result_exp = mysql_query("SELECT * FROM rou_expense_tbl WHERE route_id='".$job_id."'")
//or die("SELECT EXP ".mysql_error());

$result_exp = mysql_query("SELECT * FROM rou_expense_tbl WHERE route_id='".$job_id."'
AND acct_type='Expense'") or die("SELECT EXP ".mysql_error());

$result_inc = mysql_query("SELECT * FROM rou_expense_tbl WHERE route_id='".$job_id."'
AND acct_type='Income'") or die("SELECT EXP ".mysql_error());

$dr1_id = $row_num['dr1_id'];
$dr2_id = $row_num['dr2_id'];
$asst1_id = $row_num['asst1_id'];
$asst2_id = $row_num['asst2_id'];

$result_dr1 = mysql_query("SELECT * FROM employee_tbl WHERE autoid='".$dr1_id."'")
or die("SELECT NUM ".mysql_error());
$row_dr1 = mysql_fetch_array($result_dr1);

if($dr1_id=='0')
{
	$dr1 = 'None';
}

if($dr1_id!='0')
{
	$dr1 = $row_dr1['lname'].','.$row_dr1['fname'];
}

$result_dr2 = mysql_query("SELECT * FROM employee_tbl WHERE autoid='".$dr2_id."'")
or die("SELECT NUM ".mysql_error());
$row_dr2 = mysql_fetch_array($result_dr2);

if($dr2_id=='0')
{
	$dr2 = 'None';
}

if($dr2_id!='0')
{
	$dr2 = $row_dr2['lname'].','.$row_dr2['fname'];
}

$result_asst1 = mysql_query("SELECT * FROM employee_tbl WHERE autoid='".$asst1_id."'")
or die("SELECT NUM ".mysql_error());
$row_asst1 = mysql_fetch_array($result_asst1);

if($asst1_id=='0')
{
	$asst1 = 'None';
}

if($asst1_id!='0')
{
	$asst1 = $row_asst1['lname'].','.$row_asst1['fname'];
}

$result_asst2 = mysql_query("SELECT * FROM employee_tbl WHERE autoid='".$asst2_id."'")
or die("SELECT NUM ".mysql_error());
$row_asst2 = mysql_fetch_array($result_asst2);

if($asst2_id=='0')
{
	$asst2 = 'None';
}

if($asst2_id!='0')
{
	$asst2 = $row_asst2['lname'].','.$row_asst2['fname'];
}

$emp_id = $row_num['emp_id'];
$result_emp = mysql_query("SELECT * FROM vehicle_tbl WHERE vehicle_id='".$emp_id."'")
or die("SELECT EMP ".mysql_error());
$row_emp = mysql_fetch_array($result_emp);

$result_comp = mysql_query("SELECT * FROM company_tbl WHERE company_id='".$company_id."'")
or die("SELECT COMPANY ".mysql_error());
$row_comp = mysql_fetch_array($result_comp);
?>
<html>
<head>
<script type="text/javascript">
function print_doc()
{
window.print();
}
</script>
</head>
<body onload='print_doc()'>
<table border='0' width='810' style='font-size:14px;font-family:Times New Roman'>
<tr>
 <th colspan='6' width='100%' style='text-align:center;' colspan='3'><?php echo $row_comp['company_name']?></th>
</tr>
<tr>
 <th colspan='6' width='100%' style='text-align:center;' colspan='3'><?php echo $row_comp['address']?></th>
</tr>
<tr>
 <td width='33%' style='text-align:left;'>Start Date:<?php echo $row_num['start_date']?></td>
 <td width='34%' style='text-align:center;'><?php echo $row_comp['website']?></td>
 <td width='33%' style='text-align:right;'>Contact No:<?php echo $row_comp['comp_phone']?></td>
</tr>
<tr>
 <td width='33%' style='text-align:left;'>End Date:<?php echo $row_num['end_date']?></td>
 <td width='34%' style='text-align:center;'></td>
 <td width='33%' style='text-align:right;'>Fax No:<?php echo $row_comp['comp_fax']?></td>
</tr>
</table>
<div style="width: 100%; text-align:left;"><img border="0" src="image/rou_rlog.png" width="810" height="30" /></div>
<table border='0' width='810' style='font-size:14px;font-family:Times New Roman'>
<tr>
 <td colspan='1' width='25%' style='text-align:left;'><b>Plate No:</b> <?php echo $row_emp['veh_num']?>
 <br><b>Model:</b> <?php echo $row_emp['year'].' '.$row_emp['manufacturer_name'].' '.$row_emp['model_name']?></td>
 <td colspan='1' width='25%' style='text-align:left;'><b>Driver 1:</b> <?php echo $dr1?>
 <br><b>Assistant 1:</b> <?php echo $asst1?></td>
 <td colspan='1' width='25%' style='text-align:left;'><b>Driver 2:</b> <?php echo $dr2?>
 <br><b>Assistant 2:</b> <?php echo $asst2?></td>
</tr>
<tr><td><b>Description:</b> <?php echo $row_num['description']?></td></tr>
</table>
<table border='1' width='810' style='font-size:14px;font-family:Times New Roman'>
<tr>
 <th width='20%' rowspan='2'>Client Name</th>
 <th width='20%' rowspan='2'>Location</th>
 <th width='30%' colspan='3'>Time</th>
 <th width='30%' colspan='3'>Odometer</th>
</tr>
<tr>
 <th width='10%'>Departure</th>
 <th width='10%'>Arrival</th>
 <th width='10%'>Total</th>
 <th width='10%'>Departure</th>
 <th width='10%'>Arrival</th>
 <th width='10%'>Total</th>
</tr>
<?php
$ctr=0;
while($row_select = mysql_fetch_array($result_select))
{	
?>
<tr>
 <!--<td width='10%'>&nbsp;</td>-->
 <td width='30%'><?php echo $row_select['client']?>&nbsp;</td>
 <td width='20%'><?php echo $row_select['location']?>&nbsp;</td>
 <td width='10%' align='right'><?php echo $row_select['start_hrs'].':'.$row_select['start_min']?>&nbsp;</td>
 <td width='10%' align='right'><?php echo $row_select['stop_hrs'].':'.$row_select['stop_min']?>&nbsp;</td>
 <td width='10%' align='right'><?php echo $row_select['hrs']?>&nbsp;</td>
 <td width='10%' align='right'><?php echo $row_select['last_odometer']?>&nbsp;</td>
 <td width='10%' align='right'><?php echo $row_select['arrival_odometer']?>&nbsp;</td>
 <td width='10%' align='right'><?php echo $row_select['miles']?>&nbsp;</td>
 <!--<td width='10%'>&nbsp;</td>-->
</tr>
<?php
$db_hrs = $tot_hrs;
$decimal = strstr($db_hrs, '.');
$mins = round($decimal * 60);
$hr = $db_hrs - $decimal;
if($hr<10)
$new_hr = '0'.$hr;
if($hr>=10)
$new_hr = $hr;
$comp_hrs = $new_hr.'h'.' '.$mins.'m';
//die($comp_hrs);	
$ctr++;
}
?>
<tr>
	<td colspan='2' style='font-weight:bold;background-color:#C0C0C0;text-align:right'>&nbsp;</td>
	<td colspan='2' style='font-weight:bold;background-color:#C0C0C0;text-align:right'>Overall Total</td>
	<td colspan='1' style='background-color:#C0C0C0;text-align:right'><?php echo $comp_hrs?>&nbsp;</td>
	<td colspan='2' style='font-weight:bold;background-color:#C0C0C0;text-align:right'>Overall Total</td>
	<td colspan='1' style='background-color:#C0C0C0;text-align:right'><?php echo commafy($row_tot_od['sum_miles'])?>&nbsp;</td>
</tr>
</table>


<!-- FOR INCOME -->
<br />
<img border="0" src="image/rou_income.png" width="810" height="23"/>
<table border='1' width='810' style='font-size:14px;font-family:Times New Roman'>
<tr>
 <th width='50%' colspan='2'>Account Type</th>
 <th width='50%' colspan='8'>Amount</th>
</tr>
<?php
$ctr=0;
while($row_inc = mysql_fetch_array($result_inc))
{
$exp_t = $row_inc['type'];
$result_look = mysql_query("select * from lookup_expense_tbl where company_id = '".$_SESSION['company_id']."' AND id='".$exp_t."'");
$row_look = mysql_fetch_array($result_look);
$exp = $row_look['value'];
?>
<tr>
 <td width='50%' colspan='2'><?php echo $exp?>&nbsp;</td>
 <td width='50%' colspan='8' align='right'><?php echo commafy($row_inc['amount'])?>&nbsp;</td>
</tr>
<?php
$ctr++;
}
?>
<tr>
	<td colspan='8' width='84%' style='font-weight:bold;background-color:#C0C0C0;text-align:right'>Overall Total</td>
	<td colspan='2' width='16%' style='background-color:#C0C0C0;text-align:right'><?php echo commafy($row_income['sum_expense'])?></td>
</tr>
</table>


<br />
<img border="0" src="image/rou_expense.png" width="810" height="23"/>
<table border='1' width='810' style='font-size:14px;font-family:Times New Roman'>
<tr>
 <th width='50%' colspan='2'>Account Type</th>
 <th width='50%' colspan='8'>Amount</th>
</tr>
<?php
$ctr=0;
while($row_exp = mysql_fetch_array($result_exp))
{
$exp_t = $row_exp['type'];
$result_look = mysql_query("select * from lookup_expense_tbl where company_id = '".$_SESSION['company_id']."' AND id='".$exp_t."'");
$row_look = mysql_fetch_array($result_look);
$exp = $row_look['value'];
?>
<tr>
 <td width='50%' colspan='2'><?php echo $exp?>&nbsp;</td>
 <td width='50%' colspan='8' align='right'><?php echo commafy($row_exp['amount'])?>&nbsp;</td>
</tr>
<?php
$ctr++;
}
?>
<tr>
	<td colspan='8' width='84%' style='font-weight:bold;background-color:#C0C0C0;text-align:right'>Overall Total</td>
	<td colspan='2' width='16%' style='background-color:#C0C0C0;text-align:right'><?php echo commafy($row_expense['sum_expense'])?></td>
</tr>
</table>
<br />
<img border="0" src="image/rou_fuel.png" width="810" height="23"/>
<table border='1' width='810' style='font-size:14px;font-family:Times New Roman'>
<tr>
 <th width='26%'>Date</th>
 <th width='26%'>Brand</th>
 <th width='16%'>Price/Liter</th>
 <th width='16%'>Liter</th>
 <th width='16%'>Total</th>
</tr>
<?php
$ctr=0;
while($row_fuel_exp = mysql_fetch_array($result_fuel_exp))
{
?>
<tr>
 <td width='26%'><?php echo $row_fuel_exp['fuel_date']?>&nbsp;</td>
 <td width='26%'><?php echo $row_fuel_exp['brand']?>&nbsp;</td>
 <td width='16%' align='right'><?php echo commafy($row_fuel_exp['price'])?>&nbsp;</td>
 <td width='16%' align='right'><?php echo $row_fuel_exp['gallons']?>&nbsp;</td>
 <td width='16%' align='right'><?php echo commafy($row_fuel_exp['tot_cost'])?>&nbsp;</td>
</tr>
<?php
$ctr++;
}
?>
<tr>
	<td colspan='4' style='font-weight:bold;background-color:#C0C0C0;text-align:right'>Overall Total</td>
	<td colspan='2' style='background-color:#C0C0C0;text-align:right'><?php echo commafy($row_fuel['sum_fuel'])?></td>
</tr>
</table>
<!--<p style=" position: absolute; bottom: 0; left: 0; width: 100%; text-align: center;"><img border="0" src="image/footer_wo.png" width="135" height="30" style='margin-left:auto;margin-right:auto'/></img></p>-->
<p style=" position: relative; top : 0; width: 100%; text-align: center;"><img border="0" src="image/footer_wo.png" width="135" height="30" style='margin-left:auto;margin-right:auto'/></img></p>
</body>
</html>
<?php
?>